/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bt.dmdb.controller.impl;

/**
 *
 * @author manjunathad
 */
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.bt.dmdb.model.LoginUser;
import com.bt.dmdb.service.UsersService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.ui.ModelMap;
import javax.annotation.Resource;

@Controller
public class SecurityNavigation {
    
    @Resource
    private UsersService usersService;

    public UsersService getUsersService() {
        return usersService;
    }

    public void setUsersService(UsersService usersService) {
        this.usersService = usersService;
    }


    @RequestMapping(value = "/user-login", method = RequestMethod.GET)
    public ModelAndView loginForm() {
        return new ModelAndView("login");
    }

    @RequestMapping(value = "/error-login", method = RequestMethod.GET)
    public ModelAndView invalidLogin() {
        ModelAndView modelAndView = new ModelAndView("login");
        modelAndView.addObject("error", true);
        return modelAndView;
    }

     @RequestMapping(value = "/success-login", method = RequestMethod.GET)
    public ModelAndView successLogin(ModelMap m) {
    	String name = SecurityContextHolder.getContext().getAuthentication().getName();
    	LoginUser loginUser = usersService.getUser(name);
    	m.addAttribute("uid", loginUser.getId());
    	m.addAttribute("uname", loginUser.getFirstname());
    	m.addAttribute("urname", loginUser.getRole().getName());
        return new ModelAndView("success-login");
    }
}
